/********************************************/
  // Creation History
  // --------------------------------------------------------------
  // Company:				HTHK
  //	NAME:				MultipleOptions.java
  // Title   :				Create Multiple Options for JIS System
  //	FUNCTION:
  //	BY:					Beyes Xia
  //	DATE:				2004-1-14 15:47
  // --------------------------------------------------------------
/*********************************************/

package tss.options;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.ffpc.sql.ConnectionFactory;
import com.util.JdbcUtil;

public class MultipleOptions
{	
	private Logger logger = Logger.getLogger(MultipleOptions.class);
	private Vector optionValue = new Vector();
	private String optionHtml = "";
	private int size = 0;
		
  	public MultipleOptions(String table_name,String option_field)
  	{
  		ConnectionFactory connectionFactory = null;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		String sql = "select "+option_field+" from "+table_name;
		//logger.debug("sql="+sql);
		try
		{
	  		connectionFactory = new ConnectionFactory("fasp_tss");
	  		con = connectionFactory.getConnection();
  			logger.debug("MultipleOptions Create Connection ......");	  		
	  		stmt = con.createStatement();
	  		rs = stmt.executeQuery(sql);
	  		while(rs.next())
	  		{
	  			
	  			String[] option_value = new String[1];
	  			option_value[0] = rs.getString(1);
	  			//logger.debug(option_value[0]);
	  			optionValue.addElement(option_value);
	  			size++;
	  			//logger.debug("size:"+size);
	  		}
  		}
  		catch (Exception e)
  		{
  			logger.error("Exception:"+e.getMessage());
  		}
        finally
        {
        	JdbcUtil.release(stmt, rs);
            try
            {
                connectionFactory.finalize();
	  			logger.info("MultipleOptions free Connection ......");                
            }
            catch (Exception ex) 
            {
                logger.error("Exception:"+ex.getMessage());
            }
        }   		
  	}
  	
  	public MultipleOptions(String table_name,String option_field,boolean distinct)
  	{
  		ConnectionFactory connectionFactory = null;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "";
		
		if(distinct)
		{
			sql = "select "+option_field+" from "+table_name;
			//logger.debug("sql="+sql);
		}
		else
		{
			sql = "select distinct "+option_field+" from "+table_name;
			//logger.debug("sql="+sql);
		}
		try
		{
	  		connectionFactory = new ConnectionFactory("fasp_tss");
	  		con = connectionFactory.getConnection();
  			logger.debug("MultipleOptions Create Connection ......");	  		
	  		stmt = con.createStatement();
	  		rs = stmt.executeQuery(sql);
	  		while(rs.next())
	  		{
	  			
	  			String[] option_value = new String[1];
	  			option_value[0] = rs.getString(1);
//logger.debug(option_value[0]);
	  			optionValue.addElement(option_value);
	  			size++;
//logger.debug("size:"+size);	  			
	  		}
  		}
  		catch (Exception e)
  		{
  			logger.error("Exception:"+e.getMessage());
  		}
        finally
        {
        	JdbcUtil.release(stmt, rs);
            try
            {
                connectionFactory.finalize();
	  			logger.info("MultipleOptions free Connection ......");                
            }
            catch (Exception ex) 
            {
                logger.error("Exception:"+ex.getMessage());
            }
        }  		
  	}
  	
  	public int getSize()
  	{
  		return size;
  	}
  	
  	public Vector getVectorValue()
  	{
  		return optionValue;
  	}
  	
	public String getOptions()
	{
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<optionValue.size();i++)
		{
			String[] option_value = new String[1];
			option_value = (String[])optionValue.get(i);
			sb.append("<option value=\""+option_value[0]+"\">"+option_value[0]+"</option>\n");
		}
		optionHtml = sb.toString();
		//logger.debug("optionHtml:"+optionHtml);
		return optionHtml;
	}
	public String getOptions(String[] defaultOptions)
	{
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<optionValue.size();i++)
		{
			String[] option_value = new String[1];
			option_value = (String[])optionValue.get(i);
			boolean selected = false;
			for(int j=0;j<defaultOptions.length;j++)
			{				
				if(option_value[0].equals(defaultOptions[j]))
					selected = true;
			}
			if(!selected)
				sb.append("<option value=\""+option_value[0]+"\">"+option_value[0]+"</option>\n");
			else
				sb.append("<option value=\""+option_value[0]+"\" selected>"+option_value[0]+"</option>\n");
		}
		optionHtml = sb.toString();
//logger.debug("optionHtml:"+optionHtml);
		return optionHtml;
	}
}